<?php

/**
 * This is the model class for table "menu".
 *
 * The followings are the available columns in table 'menu':
 * @property integer $id
 * @property integer $sort
 * @property integer $parent
 * @property string $title
 * @property string $url
 * @property string $bizrule
 */
class Menu extends CActiveRecord {

    public $tree        = NULL;
    private $user       = NULL;
    private $menuGroup  = Array();
    
    function __construct($scenario='insert')
    { 
        
        $this->user         = Pegawai::model()->findByPk(Yii::app()->user->id);
        
        $this->menuGroup    = MenuGroup::model()->findAllByAttributes(
                                    array(
                                        'id_group'=>  $this->user->id_group
                                    )
                                );
    
        parent::__construct($scenario);
    }
    
    
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Menu the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'menu';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('sort', 'required'),
            array('sort, parent_id', 'numerical', 'integerOnly' => true),
            array('title, url, bizrule', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, sort, parent_id, title, url, bizrule', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'Parent' => array(self::BELONGS_TO, 'Menu', 'id'),
            'MenuGroup' => array(self::HAS_MANY, 'MenuGroup', 'id_menu'),
            'childs' => array(self::HAS_MANY, 'Menu', 'parent_id', 'order' => 'sort ASC'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'sort' => 'Sort',
            'parent_id' => 'Parent',
            'title' => 'Title',
            'url' => 'Url',
            'bizrule' => 'Bizrule',
        );
    }

    
    public function getMenuTree($parent=1, $level=0)
    {
        $criteria = new CDbCriteria;
        $criteria->condition='parent_id=:id';
        $criteria->params=array(':id'=>$parent);
        $model = $this->findAll($criteria);
       
        foreach ($model as $key) {
            $this->tree[] = array(
                'id' => $key->id,
                'sort' => $key->sort,
                'parent_id' => $key->parent_id,
                'title' => str_repeat('+------', $level+1) . $key->title,
                'url' => $key->url
            );
            $this->getMenuTree($key->id, $level+1);
        }
        
        return $this->tree;
    }
    
    public function getListed() {
        $subitems = array();
        if ($this->childs)
        {
            foreach ($this->childs as $child) {
                 foreach ($this->menuGroup as $key => $value) {
                   if($child->id==$value->id_menu) 
                        $subitems[] = $child->getListed();
                }    
              }
        }
        
        $returnarray = array('label' => $this->title, 'url' => array($this->url));
        if ($subitems != array())
            $returnarray = array_merge($returnarray, array('items' => $subitems));
        return $returnarray;
    }
    
    
    public function getMenuItems(){
        
        $items[] = $this->getListed();
      
        $subitems = array();
        if(!empty ($items)){
            foreach($items as $item)
                $subitems = $item['items'];
        }
        
        return $subitems;
    }
        
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('sort', $this->sort);
        $criteria->compare('parent_id', $this->parent_id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('url', $this->url, true);
        $criteria->compare('bizrule', $this->bizrule, true);
        $criteria->with = array('Parent');
        $criteria->select = 't.*, Parent.title as parent_title';

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

}
